// 递归实现一个对象深拷贝
const obj = {
    a: '1',
    b: [1, 2, 3],
    c: {
        h: '1'
    },
    d: function () {
        console.log(this);
    }
}

function recursion (obj){
    if (!(obj instanceof Object)){
        throw new Error('obj is not a object');
    }
    let newBoj = {};
    for (let key in obj) {
        if (obj[key] instanceof Function) {
            newBoj[key] = obj[key];
            continue;
        }

        if (obj[key] instanceof Array) {
            newBoj[key] = [...obj[key]];
            continue;
        }

        if (obj[key] instanceof Object) { 
            const res = recursion(obj[key]); // 还是返回一个新的数组回来
            newBoj[key] = res;
        } else {
            newBoj[key] = obj[key];
        }
        
    }

    return newBoj;
}

const res = recursion(obj);
obj.c.h = '2';
obj.b[0] = 4
// console.log('🚀 ~ This is a result of console.log ~ ✨: ', res);
// console.log('🚀 ~ This is a result of console.log ~ ✨: ', obj);
//  obj.d()
//  res.d()
console.log('🚀 ~ This is a result of console.log ~ ✨: ', obj.d === res.d);
// 有个问题： 函数似乎并没有被深拷贝，他们执向的依然是同一个空间
